package com.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.RewindDAO;
import com.dao.TestDAO;
import com.pojo.Rewind;
import com.pojo.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Map;

@Service
public class RewindService extends ServiceImpl<RewindDAO, Rewind> {

    @Autowired
    private RewindDAO rewindDAO;

    @Autowired
    private TestDAO testDAO;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private QuestionService questionService;

    /**
     *  添加考生的答题试卷
     * @param rewind  提交答案
     * @return
     */
    public int addRewind(Rewind rewind) {
        //得到long类型当前时间
        long l = System.currentTimeMillis();
        //new日期对
        Timestamp date = new Timestamp(l);
        rewind.setTestDate(date);
        redisTemplate.delete(rewind.getUsername());
        System.out.println(rewind.getUsername());
        int n=rewindDAO.insert(rewind);
        return n;
    }

    /**
     *  根据id 查询 考卷内容
     * @param rewindId
     * @return
     */
    public Map findByIdRewind(Integer rewindId) {
        // 根据id 查询 考卷内容
        Rewind rewind=rewindDAO.selectById(rewindId);
        // 根据id 查询试卷
        Test test=testDAO.selectById(rewind.getTestId());
        // 根据id查询试卷内容
        Map map=questionService.findByIdRewind(rewindId);
        map.put("test",test);
        map.put("rewind",rewind);
        return map;
    }
}
